package com.wish.group05.finance.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wish.group05.base.entity.CwCheckBatchDetail;
import com.wish.group05.base.service.ICwCheckBatchDetailService;
import com.wish.group05.base.service.ICwCheckBatchService;
import com.wish.group05.common.security.service.TokenService;
import com.wish.group05.finance.dto.CwCheckBatchDetailDto;
import com.wish.group05.system.api.RemoteDictDataService;
import com.wish.group05.system.api.model.LoginUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.group05.common.log.annotation.Log;
import com.wish.group05.common.log.enums.BusinessType;
import com.wish.group05.common.security.annotation.PreAuthorize;
import com.wish.group05.common.core.web.controller.BaseController;
import com.wish.group05.common.core.web.domain.AjaxResult;
import com.wish.group05.common.core.utils.poi.ExcelUtil;
import com.wish.group05.common.core.web.page.TableDataInfo;

/**
 * 对账批次明细（保存对账结果明细的）Controller
 * 
 * @author wish
 * @date 2022-05-29
 */
@Slf4j
@RestController
@RequestMapping("/detail")
public class TCwCheckBatchDetailController extends BaseController
{
    @Autowired
    ICwCheckBatchDetailService cwCheckBatchDetailService;
    @Resource
    RemoteDictDataService remoteDictDataService;
    @Autowired
    TokenService tokenService;

    /**
     * 差错处理
     * @param cwCheckBatchDetailDto
     * @return
     */
    @GetMapping("processCheckDifResult")
    public AjaxResult processCheckDifResult(CwCheckBatchDetailDto cwCheckBatchDetailDto) {
        log.info("processCheckDifResult对账差异的差错处理 Start...");
        // 数据库状态检查，防止重复处理
        CwCheckBatchDetail batchDetail = cwCheckBatchDetailService.getById(cwCheckBatchDetailDto.getBatchId());
        if (batchDetail == null) {
            return AjaxResult.error("选择对账明细记录已经不存在，请刷新画面重新处理。");
        }else {
            if (batchDetail.getProcessState() != null && batchDetail.getProcessState() != 1) {
                // 状态不是未处理的时候，提示画面
                return AjaxResult.error("选择对账明细记录已经被处理过，请勿重复处理。");
            }
        }

        //修改
        CwCheckBatchDetail cwCheckBatchDetail = new CwCheckBatchDetail();
        //封装属性
        cwCheckBatchDetail.setId(cwCheckBatchDetailDto.getBatchId());
        cwCheckBatchDetail.setProcessState(cwCheckBatchDetailDto.getProcessState());

        //获取登录用户信息
        LoginUser loginUser = tokenService.getLoginUser();
        cwCheckBatchDetail.setUpdateUserid(loginUser.getUserid());

        cwCheckBatchDetail.setUpdateTime(new Date());
        //修改
        cwCheckBatchDetailService.saveOrUpdate(cwCheckBatchDetail);
        return AjaxResult.success();

    }

    /**
     * 查询对账批次明细（保存对账结果明细的）列表
     */
    @PreAuthorize(hasPermi = "finance:detail:detaillist")
    @GetMapping("/list")
    public TableDataInfo list(CwCheckBatchDetailDto cwCheckBatchDetailDto)
    {
        QueryWrapper<CwCheckBatchDetail> wrapper = new QueryWrapper<>();
        //渠道名称模糊查询
        if (StringUtils.isNoneBlank(cwCheckBatchDetailDto.getTradeQudao())) {
            wrapper.like("trade_qudao",cwCheckBatchDetailDto.getTradeQudao());
        }
        //交易结果查询
        if (cwCheckBatchDetailDto.getTradeResult() != null) {
            wrapper.eq("trade_result",cwCheckBatchDetailDto.getTradeResult());
        }
        //对账批次查询
        if (StringUtils.isNoneBlank(cwCheckBatchDetailDto.getBatchNo())) {
            wrapper.like("batch_no",cwCheckBatchDetailDto.getBatchNo());
        }
        //交易金额查询
        if (cwCheckBatchDetailDto.getAmount() != null) {
            wrapper.eq("amount",cwCheckBatchDetailDto.getAmount());
        }
        //是否差错查询
        if (cwCheckBatchDetailDto.getHasDiff() != null) {
            wrapper.eq("has_diff",cwCheckBatchDetailDto.getHasDiff());
        }
        //交易日期查询
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (cwCheckBatchDetailDto.getTradeDateZf() != null) {
            String tradeDateZf = sdf.format(cwCheckBatchDetailDto.getTradeDateZf());
            wrapper.gt("trade_date_zf",tradeDateZf);
        }
        if (cwCheckBatchDetailDto.getTradeDateZf1() != null) {
            String tradeDateZf1 = sdf.format(cwCheckBatchDetailDto.getTradeDateZf1());
            wrapper.lt("trade_date_zf",tradeDateZf1);
        }
        //差错处理结果查询
        if (cwCheckBatchDetailDto.getProcessState() != null) {
            wrapper.eq("process_state",cwCheckBatchDetailDto.getProcessState());
        }
        //支付方式查询
        if (cwCheckBatchDetailDto.getPayWay() != null) {
            wrapper.eq("pay_way",cwCheckBatchDetailDto.getPayWay());
        }
        wrapper.eq("del_flg",0);
        startPage();
        List<CwCheckBatchDetail> list = cwCheckBatchDetailService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 获取字典表数据
     * @return
     */
    @GetMapping("getDictData")
    public AjaxResult getDictData() {
        HashMap<String, Object> map = new HashMap<>();

        //获取交易结果集合
        AjaxResult tradeResult = remoteDictDataService.getDictDataList("sys_trade_result");
        map.put("tradeResultList",tradeResult);
        //获取是否差错集合
        AjaxResult hasDiff = remoteDictDataService.getDictDataList("sys_has_diff");
        map.put("hasDiffList",hasDiff);
        //获取处理结果集合
        AjaxResult processState = remoteDictDataService.getDictDataList("sys_process_state");
        map.put("processStateList",processState);
        //获取支付方式集合
        AjaxResult payWay = remoteDictDataService.getDictDataList("sys_pay_way");
        map.put("payWayList", payWay);

        return AjaxResult.success(map);
    }

}
